<template>
	<div class="add"><el-button type="primary" @click="add">添加</el-button></div>
	<el-dialog
			    v-model="dialogVisible_add"
			    title="Tips"
			    width="500"
			  >
			    <el-input type="text" v-model="name" placeholder="请输入添加的sku名"></el-input>
			    <el-input type="text" v-model="price" placeholder="请输入添加的sku价格"></el-input>
			    <el-input type="text" v-model="num" placeholder="请输入添加的sku库存"></el-input>
				<el-select
				      v-model="goodsValue"
				      placeholder="请选择商品"
				      size="large"
				      style="width: 240px"
				    >
				      <el-option
				        v-for="item in options"
				        :key="item.id"
				        :label="item.name"
				        :value="item.id"
				      />
				    </el-select>
					<div>是否默认sku<el-switch v-model="selected" /></div>
			    <template #footer>
			      <div class="dialog-footer">
			        <el-button @click="dialogVisible_add = false">Cancel</el-button>
			        <el-button type="primary" @click="addSubmit">
			          Confirm
			        </el-button>
			      </div>
			    </template>
		</el-dialog>
	<el-dialog
		    v-model="dialogVisible_update"
		    title="Tips"
		    width="500"
		  >
		 <el-input type="text" v-model="name" placeholder="请输入添加的sku名"></el-input>
		 <el-input type="text" v-model="price" placeholder="请输入添加的sku价格"></el-input>
		 <el-input type="text" v-model="num" placeholder="请输入添加的sku库存"></el-input>
		 <el-select
		       v-model="goodsValue"
		       placeholder="请选择商品"
		       size="large"
		       style="width: 240px"
		     >
		       <el-option
		         v-for="item in options"
		         :key="item.id"
		         :label="item.name"
		         :value="item.id"
		       />
		     </el-select>
			 <div>是否默认sku<el-switch v-model="selected" /></div>
		    <template #footer>
		      <div class="dialog-footer">
		        <el-button @click="dialogVisible_update = false">Cancel</el-button>
		        <el-button type="primary" @click="updateSubmit">
		          Confirm
		        </el-button>
		      </div>
		    </template>
		  </el-dialog>
		  <el-dialog
		    v-model="dialogVisible_del"
		    title="Tips"
		    width="500"
		  >
		    确认要删除 {{delName}} 吗？
		    <template #footer>
		      <div class="dialog-footer">
		        <el-button @click="dialogVisible_del = false">Cancel</el-button>
		        <el-button type="primary" @click="delSubmit">
		          Confirm
		        </el-button>
		      </div>
		    </template>
		  </el-dialog>
	<el-table :data="tableData" style="width: 1200px;">
	<el-table-column prop="gName" label="商品名"></el-table-column>
	  <el-table-column prop="name" label="sku名" width="180" />
	  <el-table-column label="默认">
		  <template #default="scope">
			 {{scope.row.selected}}
		  </template>
	  </el-table-column>
	  <el-table-column prop="price" label="价格"></el-table-column>
	  <el-table-column prop="num" label="库存"></el-table-column>
	  <el-table-column prop="createTime" label="创建时间" />
	  <el-table-column prop="updateTime" label="更新时间" />
			<el-table-column fixed="right" width="180">
				<template #header>操作</template>
				<template #default="scope">
				  <el-button type="warning" @click="update(scope.row)">编辑</el-button>
				  <el-button type="danger" @click="del(scope.row.id,scope.row.name)">删除</el-button>
				</template>
			</el-table-column>
	</el-table>
</template>

<script setup>
	import {ref} from 'vue'
	const tableData = ref([])
	import {http} from "../components/http.js"
	function query(){
		http({url:"/querySku",method:"get"}).then(res=>{
			tableData.value = res.data.data
		})
	}
	query()
	const dialogVisible_add = ref(false)
	const dialogVisible_update = ref(false)
	const dialogVisible_del = ref(false)
	const updateId = ref(undefined)
	const delId = ref(undefined)
	const options = ref([])
	const selected = ref(0)
	const name = ref("")
	const price = ref(undefined)
	const num = ref(undefined)
	function add(){
		dialogVisible_add.value = true
		http({url:"/queryGoods",method:"get"}).then(res=>{
			options.value = res.data.data
		})
	}
	function addSubmit(){
		http({url:"/addSku",method:"post",data:{name:name.value,num:num.value,price:price.value,num:num.value,selected:selected.value,goodsValue:goodsValue.value}}).then(res=>{
			dialogVisible_add.value = false
			query()
		})
	}
	const delName = ref("")
	function del(id,name){
		dialogVisible_del.value = true
		delId.value = id
		delName.value = name
	}
	function delSubmit(){
		http({url:"/delSku?id="+delId.value,method:"get"}).then(res=>{
			dialogVisible_del.value = false
			query()
		})
	}
	const updateName = ref("")
	const goodsValue = ref(undefined)
	function update(r){
		dialogVisible_update.value = true
		updateId.value = r.id
		name.value = r.name
		price.value = r.price
		num.value = r.num
		selected.value = r.selected
		http({url:"/queryGoods",method:"get"}).then(res=>{
			options.value = res.data.data
			goodsValue.value = r.goodsId
		})
	}
	function updateSubmit(){
		http({url:'/updateSku',method:"post",data:{id:updateId.value,name:name.value,num:num.value,price:price.value,num:num.value,selected:selected.value,goodsId:goodsValue.value}}).then(res=>{
			dialogVisible_update.value = false
			query()
		})
	}
</script>

<style>
</style>